<?php

/**
 * This is the model class for table "Project".
 *
 * The followings are the available columns in table 'Project':
 * @property integer $id
 * @property string $project_name
 * @property string $point_person
 * @property string $start_date
 * @property string $end_date
 * @property double $total_cost
 * @property double $total_expenses
 * @property integer $user_id
 *
 * The followings are the available model relations:
 * @property User $user
 * @property ProjectItem[] $projectItems
 * @property PullOut[] $pullOuts
 * @property PurchaseOrder[] $purchaseOrders
 */
class Project extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'Project';
	}
	
	

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, project_name', 'required'),
			array('user_id', 'numerical', 'integerOnly'=>true),
			array('total_cost, total_expenses', 'numerical'),
			array('project_name, point_person', 'length', 'max'=>45),
			array('start_date, end_date', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, project_name, point_person, start_date, end_date, total_cost, total_expenses, user_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'projectItems' => array(self::HAS_MANY, 'ProjectItem', 'project_id'),
			'pullOuts' => array(self::HAS_MANY, 'PullOut', 'project_id'),
			'purchaseOrders' => array(self::HAS_MANY, 'PurchaseOrder', 'project_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'project_name' => 'Project Name',
			'point_person' => 'Lead Man',
			'start_date' => 'Start Date',
			'end_date' => 'End Date',
			'total_cost' => 'Total Project Cost',
			'total_expenses' => 'Total Expenses',
			'user_id' => 'User',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('project_name',$this->project_name,true);
		$criteria->compare('point_person',$this->point_person,true);
		$criteria->compare('start_date',$this->start_date,true);
		$criteria->compare('end_date',$this->end_date,true);
		$criteria->compare('total_cost',$this->total_cost);
		$criteria->compare('total_expenses',$this->total_expenses);
		$criteria->compare('user_id',$this->user_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Project the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
